<?php
namespace app\admin\controller;
use think\Db;
class Article extends Base
{   
    public function lists(){



        if( is_post() ){

            $page     = input('page');
            $status   = input('status');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            $sql = "
                select
                    [1]
                from
                    article
                where 
                    1 = ?
                ";
            $param = [1];
            
            
            if ( $status != '100all' ) {
                $sql .= " and status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and title like ?";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and create_time <= ?";
                $param[] = strtotime($max_time);
            }
            #分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
			$page_data   = com_page_data($total, $page,  $page_size);
			
			#数据
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by sort desc, id desc limit $start, $page_size";
			$field       = " id,title,pic,views,sort,status,create_time ";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);

            #格式化
            foreach($list as $k => $v){                      
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                $list[$k]['sort']           =  com_page( ['url'=>url("c/edit"),'val'=>$v['sort'],'post'=>['t'=>'article','id'=>$v['id'],'f'=>'sort']],'input' );
                $list[$k]['views']          =  com_page( ['url'=>url("c/edit"),'val'=>$v['views'],'post'=>['t'=>'article','id'=>$v['id'],'f'=>'views']],'input' );
                $list[$k]['status']         =  com_page( ['url'=>url("c/edit"),'val'=>$v['status'],'post'=>['t'=>'article','id'=>$v['id'],'f'=>'status']],'iphone' );
                
                $list[$k]['pic']            =  com_format_pic(com_get_pic($v['pic'],'small'));                
                
                //操作按钮
                $list[$k]['action']          =  com_page(['title'=>'编辑文章','url'=>url("article/edit","id={$v['id']}"),'ico'=>'edit']);
                $list[$k]['action']         .=  com_page(['title'=>'拷贝文章','url'=>url("c/copy"),'post'=>['t'=>'article','id'=>$v['id']],'ico'=>'copy']);
                
                    $list[$k]['action']         .=  com_page(['title'=>'删除文章','url'=>url("c/del"),'post'=>['t'=>'article','id'=>$v['id']],'ico'=>'del3', 'confirm'=>"你确定要删除[{$v['title']}]吗?"]);
                
                $list[$k]['action']          .=  com_page(['title'=>'编辑文章','url'=>url("article/edit","id={$v['id']}"),'ico'=>'']);


            }
			$com_body    = com_build_table($list, array("checkbox","id","title","pic","views", "sort","status" ,"create_time", "action"));
			$com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data,'suc'=>['js_action'=>'function.iphone']]);
        } 

        $data = [
            'status'    => ['100all'=>'全部状态'] + c('type.status'),
            'nav'       => ['文章管理', '列表'],
        ];
        return view('', $data);
    }
    




    //添加
    public function add(){
        if( is_post() ){
            $d = com_data(input());
            $rule = [
                'title'      =>  'require',
            ];
            $msg = [
                'title.require'      =>  '标题不能为空',
            ];
            com_validate( $rule, $msg, $d, true );
            $d['create_time'] = time();
            Db::name('article')->insert($d);
            $config = [
                'msg' => '添加成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }    
        $all= [
            '100all' => [
                'id' => '100all',
                'parent_id' => 0,
                'depath' => 0,
                ]
        ];
        $data = [
            
        ];
        return view('', $data);
    }



    //编辑
    public function edit( $id ){
        if( is_post() ){
            $d = com_data(input());
            $rule = [
                   'title'=>'require',
                ];
            $msg = [
                   'title.require'=>'标题不能为空',
                ];
            com_validate( $rule, $msg, $d, true );
            $d['id'] = $id;
            $d['update_time'] = time();
            Db::name('article')->update($d);
            $config = [
                'suc' => [
                    'msg' => '编辑成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        
        $data = [
            'info'      => Db::name('article')->find($id),
        ];
        return view('', $data);
    }



}